package com.shruijin.wy.thread.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Slf4j
public class CountDownLatchExample2 {

    private final static int totalCount=1000;


    public static void main(String[] args) throws InterruptedException {
        ExecutorService exec = Executors.newCachedThreadPool();
        final CountDownLatch countDownLatch = new CountDownLatch(totalCount);

        for (int i=0;i<totalCount;i++){
            final  int threadNum  = i;
            exec.execute(()->{
                try{
                    test2(threadNum);
                } catch (Exception e){
                    log.error("exception {}",e);
                } finally {
                    countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await(10, TimeUnit.MILLISECONDS);
        log.info("finished!");
        exec.shutdown();
    }

    private  static void test2(int threadNum) throws Exception{
        Thread.sleep(1);
        log.info("test thread number is :{}",threadNum);
        //Thread.sleep(100);
    }
}
